package cn.edu.baiyunu.myapplication.ui.home

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.AutoFixHigh
import androidx.compose.material.icons.filled.Crop
import androidx.compose.material.icons.filled.Download
import androidx.compose.material.icons.filled.Lock
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.shadow
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.text.ExperimentalTextApi
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import cn.edu.baiyunu.myapplication.ui.components.FeatureCard

/**
 * 功能特点部分组件 - 按照原型图样式重新设计
 */
@OptIn(ExperimentalTextApi::class)
@Composable
fun FeaturesSection(
    features: List<HomeViewModel.Feature>,
    modifier: Modifier = Modifier
) {
    Card(
        modifier = modifier.fillMaxWidth(),
        colors = CardDefaults.cardColors(
            containerColor = Color.White
        ),
        elevation = CardDefaults.cardElevation(
            defaultElevation = 2.dp,
            pressedElevation = 0.dp
        ),
        shape = RoundedCornerShape(24.dp)
    ) {
        Column(
            modifier = Modifier.padding(24.dp)
        ) {
            // 标题
            Text(
                text = "功能特点",
                style = TextStyle(
                    brush = Brush.horizontalGradient(
                        colors = listOf(
                            Color(0xFF4F46E5),
                            Color(0xFF7E22CE)
                        )
                    ),
                    fontSize = MaterialTheme.typography.titleLarge.fontSize,
                    fontWeight = FontWeight.SemiBold
                ),
                modifier = Modifier.padding(bottom = 24.dp)
            )
            
            // 功能列表（网格布局）- 使用固定高度确保所有内容显示
            LazyVerticalGrid(
                columns = GridCells.Fixed(2),
                modifier = Modifier
                    .fillMaxWidth()
                    .height(320.dp), // 增加高度确保内容完全显示
                userScrollEnabled = false
            ) {
                items(features) { feature ->
                    FeatureCardGlass(feature = feature)
                }
            }
        }
    }
}

/**
 * 玻璃效果功能特点卡片
 */
@Composable
private fun FeatureCardGlass(
    feature: HomeViewModel.Feature,
    modifier: Modifier = Modifier
) {
    // 根据特点名称选择对应的图标和颜色
    val (icon, cardStyle) = when (feature.iconName) {
        "magic" -> Icons.Default.AutoFixHigh to CardStyle.PRIMARY
        "crop-alt" -> Icons.Default.Crop to CardStyle.ROSE
        "download" -> Icons.Default.Download to CardStyle.AMBER
        "shield-alt" -> Icons.Default.Lock to CardStyle.PRIMARY
        else -> Icons.Default.AutoFixHigh to CardStyle.PRIMARY
    }
    
    // 左边框颜色
    val borderColor = when (cardStyle) {
        CardStyle.PRIMARY -> Color(0xFF4F46E5) // indigo-600
        CardStyle.ROSE -> Color(0xFFF43F5E) // rose-500
        CardStyle.AMBER -> Color(0xFFF59E0B) // amber-500
    }
    
    // 卡片背景颜色 - 调整透明度为更低
    val backgroundBrush = when (cardStyle) {
        CardStyle.PRIMARY -> Brush.linearGradient(
            colors = listOf(
                Color(0xFF4F46E5).copy(alpha = 0.05f),
                Color(0xFF7E22CE).copy(alpha = 0.05f)
            )
        )
        CardStyle.ROSE -> Brush.linearGradient(
            colors = listOf(
                Color(0xFFF43F5E).copy(alpha = 0.05f),
                Color(0xFFD946EF).copy(alpha = 0.05f)
            )
        )
        CardStyle.AMBER -> Brush.linearGradient(
            colors = listOf(
                Color(0xFFF59E0B).copy(alpha = 0.05f),
                Color(0xFFF97316).copy(alpha = 0.05f)
            )
        )
    }
    
    Box(
        modifier = modifier
            .padding(8.dp)
            .fillMaxWidth()
            // 添加轻微的阴影效果
            .shadow(
                elevation = 1.dp,
                shape = RoundedCornerShape(16.dp),
                ambientColor = Color.Black.copy(alpha = 0.02f),
                spotColor = Color.Black.copy(alpha = 0.04f)
            )
            .clip(RoundedCornerShape(16.dp))
            .background(backgroundBrush)
            // 添加左侧边框，模拟原型图中的card-gradient-1/2/3样式
            .padding(start = 3.dp)
            .padding(16.dp)
    ) {
        // 左侧边框
        Box(
            modifier = Modifier
                .width(3.dp)
                .height(100.dp)
                .background(borderColor)
                .align(Alignment.CenterStart)
                .padding(end = 16.dp)
        )
        
        Column(
            modifier = Modifier.padding(start = 16.dp)
        ) {
            // 图标背景
            Box(
                modifier = Modifier
                    .size(56.dp)
                    .clip(RoundedCornerShape(12.dp))
                    // 使用对应颜色的浅色背景
                    .background(
                        brush = when (cardStyle) {
                            CardStyle.PRIMARY -> Brush.linearGradient(
                                colors = listOf(
                                    Color(0xFF4F46E5).copy(alpha = 0.05f),
                                    Color(0xFF7E22CE).copy(alpha = 0.05f)
                                )
                            )
                            CardStyle.ROSE -> Brush.linearGradient(
                                colors = listOf(
                                    Color(0xFFF43F5E).copy(alpha = 0.05f),
                                    Color(0xFFD946EF).copy(alpha = 0.05f)
                                )
                            )
                            CardStyle.AMBER -> Brush.linearGradient(
                                colors = listOf(
                                    Color(0xFFF59E0B).copy(alpha = 0.05f),
                                    Color(0xFFF97316).copy(alpha = 0.05f)
                                )
                            )
                        }
                    )
                    .padding(16.dp),
                contentAlignment = Alignment.Center
            ) {
                // 图标使用渐变色
                Icon(
                    imageVector = icon,
                    contentDescription = feature.title,
                    tint = when (cardStyle) {
                        CardStyle.PRIMARY -> Color(0xFF4F46E5)
                        CardStyle.ROSE -> Color(0xFFF43F5E)
                        CardStyle.AMBER -> Color(0xFFF59E0B)
                    },
                    modifier = Modifier.size(24.dp)
                )
            }
            
            // 标题
            Text(
                text = feature.title,
                style = MaterialTheme.typography.titleMedium,
                fontWeight = FontWeight.SemiBold,
                color = Color(0xFF1E293B), // slate-800
                modifier = Modifier.padding(top = 12.dp)
            )
            
            // 描述
            Text(
                text = feature.description,
                style = MaterialTheme.typography.bodySmall,
                color = Color(0xFF475569), // slate-600
                modifier = Modifier.padding(top = 4.dp)
            )
        }
    }
}

private enum class CardStyle {
    PRIMARY, ROSE, AMBER
} 